﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.ViewModel;
using System.Collections.ObjectModel;
using PetroMentor.Service;
using DIST.LR.ZHJG.Events;
using HSE.Events;
using System.Text;
using PetroMentor.Service.Entities;
using Newtonsoft.Json;

namespace HSE.Common
{
    public class ManageBaseViewModel: NotificationObject
    {


        #region

        private T_CFG_DEPT _selectDept;
        public T_CFG_DEPT SelectDept
        {
            get
            {
                return this._selectDept;
            }
            set
            {
                if (this._selectDept != value)
                {
                    this._selectDept = value;
                    this.RaisePropertyChanged("SelectDept");
                }
            }
        }


        private DateTime _querystartTime= DateTime.Now.AddDays(-7);
        public DateTime QueryStartTime
        {
            get {
                return this._querystartTime;
            }

            set
            {
            	this._querystartTime = value;
                this.RaisePropertyChanged("QueryStartTime");
            }
        }

        private DateTime _querysendTime = DateTime.Now;
        public DateTime QueryEndTime
        {
            get
            {
                return this._querysendTime;
            }

            set
            {
                this._querysendTime = value;
                this.RaisePropertyChanged("QueryEndTime");
            }
        }

        #endregion

        public delegate void SaveHandler(SaveStateInfo info);
        public event SaveHandler SaveCompleted;
        public ButtonInfo QueryButtonInfo
        {
            get;
            set;
        }

        public ManageBaseViewModel()
        {
            this.Department = new ObservableCollection<T_CFG_DEPT>();
            this.Department.Add(new T_CFG_DEPT() { ID = "1", NAME = "采油技术服务公司" });
            this.Department.Add(new T_CFG_DEPT() { ID = "2", NAME = "油田建设工程公司" });
            this.Department.Add(new T_CFG_DEPT() { ID = "3", NAME = "生产经营部" });
            this.Department.Add(new T_CFG_DEPT() { ID = "4", NAME = "配餐服务公司" });
            this.Department.Add(new T_CFG_DEPT() { ID = "5", NAME = "办公室" });
            this.Department.Add(new T_CFG_DEPT() { ID = "6", NAME = "海油发展演示班组" });

            this.Xslx = new ObservableCollection<T_CFG_CONFIG>();
            this.Xslx.Add(new T_CFG_CONFIG() { KEY = "1", VALUE = "年初出队宣誓" });
            this.Xslx.Add(new T_CFG_CONFIG() { KEY = "2", VALUE = "特殊作业宣誓" });
            this.Xslx.Add(new T_CFG_CONFIG() { KEY = "3", VALUE = "班前会宣誓" });
            this.Xslx.Add(new T_CFG_CONFIG() { KEY = "4", VALUE = "新员工入队" });


            this.HasAllDepartment = this.Department;
            this.HasAllXslx = this.Xslx;
            this.HasAllDepartment.Insert(0, new T_CFG_DEPT() { ID = "0", NAME = "全部" });
            this.HasAllXslx.Insert(0, new T_CFG_CONFIG() { ID = "0", VALUE = "全部" });

            this.QueryButtonInfo = new ButtonInfo() { Alias = "查询", Icon = "/HSE;component/Images/btn_query.png" };

        }
        public ObservableCollection<T_CFG_CONFIG> Xslx { get; set; }


        public ObservableCollection<T_CFG_DEPT> HasAllDepartment { get; set; }

        public ObservableCollection<T_CFG_CONFIG> HasAllXslx { get; set; }

        /// <summary>
        /// 部门集合
        /// </summary>
        private ObservableCollection<T_CFG_DEPT> _dept;
        public ObservableCollection<T_CFG_DEPT> Department
        {
            get
            {
                return this._dept;
            }

            set
            {
                if (this._dept != value)
            	    this._dept = value;
                this.RaisePropertyChanged("Department");
            }
        }


        public void Save(string saveContent)
        {
            string url = string.Format("{0}SaveHandler.ashx", SystemVar.ServiceUrl);

            WebClient wc = new WebClient();
            //wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
            wc.Headers["Content-type"] = "application/json";
            wc.Encoding = Encoding.UTF8;
            wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);
            wc.UploadStringAsync(new Uri(url), "POST", saveContent);
        }


        void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            SaveStateInfo info = JsonConvert.DeserializeObject<SaveStateInfo>(e.Result);

            if (this.SaveCompleted != null)
            {
                this.SaveCompleted(info);
            }


            // throw new NotImplementedException();
        }
    }
}
